package com.sqsf.controller;

/**
 * @program: IDEA
 * @description: Project
 * @author: liuhao
 * @create: 2025/6/18 17:15
 */

import com.sqsf.javabean.User;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String redirectToUsers() {
        return "redirect:/users";
    }

    @GetMapping("/users")
    public String listUsers(Model model) {
        List<User> users = userService.selectAllUsers();
        model.addAttribute("users", users);
        return "userList";
    }


    @GetMapping("/addUser")
    public String toAddPage() {
        return "addUser";
    }

    @PostMapping("/addUser")
    public String addUser(User user) {
        userService.insertUser(user);
        return "redirect:/users";
    }
}
